package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Package;
import data.PackagePrice;
import data.RoomType;

public class PackagePriceLogic extends AbstractBL {
	public PackagePriceLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<PackagePrice> GeneratePackagePrice(long hotelId)
	{
		RoomTypeLogic roomTypeLogic = new RoomTypeLogic(req,resp);
		long iSuite = roomTypeLogic.GetRoomType(hotelId,"iSuite").getId();
		long iDeluxeKingbed = roomTypeLogic.GetRoomType(hotelId,"iDeluxe King Bed").getId();
		long iDeluxeQueenbed = roomTypeLogic.GetRoomType(hotelId,"iDeluxe Queen Bed").getId(); 
		
		List<PackagePrice> list = new ArrayList<PackagePrice>();
		
		PackagePrice packageprice;
		if(GetPackagePrice(hotelId,String.valueOf(iSuite),700,300,50) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iSuite));
			packageprice.setPriceperNight(700);
			packageprice.setExtrabed(300);
			packageprice.setUpchargePerson(50d);
			list.add(packageprice);
		}
		if(GetPackagePrice(hotelId,String.valueOf(iDeluxeKingbed),400,300,50) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iDeluxeKingbed));
			packageprice.setPriceperNight(400);
			packageprice.setExtrabed(300);
			packageprice.setUpchargePerson(50d);
			list.add(packageprice);
		}
		if(GetPackagePrice(hotelId,String.valueOf(iDeluxeQueenbed),400,300,50) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iDeluxeQueenbed));
			packageprice.setPriceperNight(400);
			packageprice.setExtrabed(300);
			packageprice.setUpchargePerson(50d);
			list.add(packageprice);
		}
		if(GetPackagePrice(hotelId,String.valueOf(iSuite),1500,800,100) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iSuite));
			packageprice.setPriceperNight(1500);
			packageprice.setExtrabed(800);
			packageprice.setUpchargePerson(100);
			list.add(packageprice);
		}
		if(GetPackagePrice(hotelId,String.valueOf(iDeluxeKingbed),1300,200,100) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iDeluxeKingbed));
			packageprice.setPriceperNight(1300);
			packageprice.setExtrabed(200);
			packageprice.setUpchargePerson(100);
			list.add(packageprice);
		}
		if(GetPackagePrice(hotelId,String.valueOf(iDeluxeQueenbed),1000,500,100) == null){
			packageprice = new PackagePrice();
			packageprice.setHotelId(hotelId);
			packageprice.setRoomTypeString(String.valueOf(iDeluxeQueenbed));
			packageprice.setPriceperNight(1000);
			packageprice.setExtrabed(500);
			packageprice.setUpchargePerson(100);
			list.add(packageprice);
		}
		
		return list;
	}
	
	public PackagePrice GetPackagePrice(long id)
	{
		Key key = KeyFactory.createKey(EntityName.PackagePrice, id);
		try {
			Entity lang = datastore.get(key);
			PackagePrice c = new PackagePrice();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public PackagePrice GetPackagePrice(long hotelId,String roomTypeId,double priceperNight,double Extrabed,double UpchargePerson)
	{
		Query q = new Query(EntityName.PackagePrice);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterRoomTypeId = new FilterPredicate("roomTypeId",FilterOperator.EQUAL,roomTypeId);
		Filter filterDouble = new FilterPredicate("priceperNight",FilterOperator.EQUAL,priceperNight);
		Filter filterExtrabed = new FilterPredicate("Extrabed",FilterOperator.EQUAL,Extrabed);
		Filter filterUpchargePerson = new FilterPredicate("UpchargePerson",FilterOperator.EQUAL,UpchargePerson);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterRoomTypeId, filterDouble,filterExtrabed,filterUpchargePerson);
		
		q.setFilter(filter);
		
		return GetPackagePrice(datastore.prepare(q).asSingleEntity());
	}
	
	public List<PackagePrice> GetAllPackagePrice(long hotelId)
	{
		Query q = new Query(EntityName.PackagePrice);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		q.setFilter(filterHotel);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public PackagePrice GetPackagePrice(Entity entity)
	{
		if(entity == null) return null;
		PackagePrice item = new PackagePrice();
		item.setEntity(entity);
		return item;
	}
	
	public List<PackagePrice> GetList(List<Entity> entities)
	{
		List<PackagePrice> list = new ArrayList<PackagePrice>();
		for(Entity e : entities)
		{
			PackagePrice a = new PackagePrice();
			
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public List<Double> GetPackagePrice(RoomType roomtype,Package packages){
		double pricepernight = 0;
		double extrabedprice = 0;
		double upchargeperson =0;
		List<PackagePrice> priceList = packages.getPackagepriceList();
		for(int i=0;i<priceList.size();i++){
			if(roomtype.getId() == priceList.get(i).getRoomtype().getId()){
				pricepernight = priceList.get(i).getPriceperNight();
				extrabedprice = priceList.get(i).getExtrabed();
				upchargeperson = priceList.get(i).getUpchargePerson();
				break;
			}
		}
		List<Double> list = new ArrayList<Double>();
		list.add(pricepernight);
		list.add(extrabedprice);
		list.add(upchargeperson);
		return list;
	}
	
	public PackagePrice AddPackagePrice(long hotelId,double extrabed,double upchardgeperperson
			,double pricepernight,String roomtypid) throws EntityNotFoundException{
		PackagePrice t = new PackagePrice();
		t.setHotelId(hotelId);
		t.setExtrabed(extrabed);
		t.setPriceperNight(pricepernight);
		t.setRoomTypeString(roomtypid);
		t.setUpchargePerson(upchardgeperperson);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
		
	}
}

